<template>
  <div class="centeet">
    <div class="top">
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-item label="设施/活动：">
                <a-input v-model="form.name" placeholder="请输入" allowClear></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="排放源：">
                <a-input v-model="form.source" placeholder="请输入" allowClear></a-input>
              </a-form-item>
            </a-col>
            <span style="float: right; overflow: hidden" class="table-page-search-submitButtons">
              <a-col :span="6">
                <a-button type="primary" @click="userPageList(1)">查询</a-button>
                <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
              </a-col>
            </span>
          </a-row>
        </a-form>
      </div>
    </div>

    <div class="tables">
      <a-button type="primary" @click="handleAdd" class="m_mb24" icon="plus">新增设施/活动</a-button>
      <a-table size="middle" :columns="columns" :data-source="listData" bordered @change="onchange"
               :pagination="pagination">
        <template slot="carbonMolecule" slot-scope="text, record, index">
          <div class="editable-row-operations">
            {{ record.carbonMolecule }}/{{ record.carbonDenomintor }}
          </div>
        </template>
        <template slot="operation" slot-scope="text, record, index">
          <div class="editable-row-operations">
            <template v-if="record.status == 0">
              <a @click="() => savePublist(record)">发布</a>
            </template>
            <template v-else>
              <a @click="() => saveCancel(record)">取消发布</a>
            </template>
            <a-divider type="vertical"/>
            <a @click="() => saveUpdate(record.id)"
            >编辑</a
            >
            <a-divider type="vertical"/>
            <a @click="() => saveDelte(record.id)">删除</a>
          </div>
        </template>
      </a-table>
    </div>
    <!--添加的新增详情弹框 -->
    <basicDrawer
      v-if="basicDrawerVisible"
      addUrl="/orgCarbonEmissionSource/addSystemFromAdmin"
      editUrl="/orgCarbonEmissionSource/addSystemFromAdmin"
      :basicDrawerVisible="basicDrawerVisible"
      :basicDrawerString="basicDrawerString"
      :basicDrawerList="basicDrawerList"
      @onCloseBasicDrawerVisible="onCloseBasicDrawerVisible"
    />
  </div>
</template>
<script>
import basicDrawer from "@comp/common/basicDrawer.vue";
import {getAction, postAction, deleteAction} from "@api/manage";
import {cloneObject} from "@/utils/util";

export default {
  name: "equipment",
  data() {
    return {
      basicDrawer: "新增设施/活动",
      visible: false,
      basicDrawerVisible: false,
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      columns: [
        {
          title: "设施/活动",
          dataIndex: "name",
          align: "center",
        },
        {
          title: "发布状态",
          dataIndex: "status_dictText",
          align: "center",
        },
        {
          title: "排放源",
          dataIndex: "source",
          align: "center",
        },
        {
          title: "排放类型",
          dataIndex: "type_dictText",
          align: "center",
        },
        {
          title: "CO₂e排放因子值",
          dataIndex: "carbonData",
          align: "center",
        },
        {
          title: "排放因子单位",
          dataIndex: "carbonMolecule",
          scopedSlots: {customRender: "carbonMolecule"},
          align: "center",
        },
        {
          title: "操作",
          align: "center",
          dataIndex: "operation",
          scopedSlots: {customRender: "operation"},
        },
      ],
      form:{},
      pagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条';
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
      },
      listData: [],
      basicDrawerList: {},
      basicDrawerString: "",
    };
  },
  components: {
    basicDrawer,
  },
  mounted() {
    this.userPageList();
  },
  methods: {
    /**
     * 删除
     */
     saveDelte(id) {
      getAction("/orgCarbonEmissionSource/deleteById", { id: id }).then((res) => {
        if (res.success) {
          this.$message.success(res.message);
          this.userPageList(1);
        }
      });
    },
    /**
     * 编辑设备
     */
    saveUpdate(value) {
      console.log(value);
      getAction("/orgCarbonEmissionSource/getDetail", {id: value}).then((res) => {
        if (res.success) {
          this.basicDrawerList = res.result;
          this.basicDrawerString = "编辑设施/活动";
          this.basicDrawerVisible = true;
        }
      });
    },
    onCloseBasicDrawerVisible() {
      this.basicDrawerVisible = false;
      this.userPageList(1);
    },
    /**
     * 设施-活动管理-用户列表
     */
    userPageList(page) {
      if (page === 1) {
        this.pagination.current = 1;
      }
      let reqData = cloneObject(this.form)
      reqData.pageNo = this.pagination.current
      reqData.pageSize = this.pagination.pageSize
      getAction("/orgCarbonEmissionSource/pageList", reqData).then((res) => {
        if (res.success) {
          this.listData = res.result.records || [];
          this.pagination.total = res.result.total;
          this.visible = true;
        }
      });
    },
    searchQuery() {
      this.userPageList(1);
    },
    searchReset() {
      this.form = {};
      this.userPageList(1);
    },
    saveCancel(value) {
      getAction("/orgCarbonEmissionSource/cancelPublish", {
        id: value.id,
      }).then((res) => {
        if (res.success) {
          this.$message.success("操作成功")
          this.userPageList(1);
        } else {
          this.$message.error(res.message)
        }
      });
    },
    savePublist(value) {
      getAction("/orgCarbonEmissionSource/publish", {
        id: value.id,
      }).then((res) => {
        if (res.success) {
          this.$message.success("操作成功")
          this.userPageList(1);
        } else {
          this.$message.error(res.message)
        }
      });
    },
    //分页
    onchange(e) {
      this.pagination = e;
      this.userPageList();
    },
    //打开抽屉
    handleAdd() {
      this.basicDrawerString = "新增设施/活动";
      this.basicDrawerList = {};
      this.basicDrawerVisible = true;
    },
    //关闭
    onClose() {
      this.visible = false;
    },
  },
};
</script>
<style lang="less" scoped>
.centeet {
  padding: 24px;
}

.color1 {
  color: #888888;
}

.inpts {
  width: 80px !important;
}

.tianjia {
  width: 380px;
  height: 156px;
  background: #f8f8f8;
  border-radius: 3px 3px 3px 3px;
  border: 1px solid #d9d9d9;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  .color {
    color: #888888;
  }

  .leftShoudong {
    background: #fff !important;
    border-color: #148958 !important;
    color: #148958 !important;
  }

  .marRight {
    margin-left: 16px;
  }
}

.tables {
  padding: 24px;
  background: #ffffff;
  border-radius: 3px 3px 3px 3px;
  border: 1px solid #eeeeee;
  margin-top: 24px;
}

.top {
  background: #ffffff;
  border-radius: 3px 3px 3px 3px;
  padding: 24px 24px 0;
}
</style>
